<?php
/**
 * --------------------------------------------------------------------
 * MCore框架 
 * 作者：阿枫，QQ：52037872，Email：nczy520@gmail.com
 * --------------------------------------------------------------------
 * 
 * SVN revision information:
 * @version $Revision: 50 $:
 * @author  $Author: zy $:
 * @date      $Date: 2013-11-09 19:34:15 +0800 (星期六, 09 十一月 2013) $:
 * @url         $HeadURL: svn://vps.mdeve.com/web_mcore/manage/function/global.func.php $:
 */


function _is_administrator() {
    global $_M;

    if(!sizeof($_M->userinfo)) return false;
    if(!isset($_M->userinfo["id"])) return false;

    return in_array($_M->userinfo["id"], $_M->cfg["Administrator"]);

}

function _initialization(){
    global $_M;
    // 定义默认系统权限
    $_M->public_permissions = array(
        "manage,login" => "允许登录后台 <small class='text-danger'> 必须要有此权限才能登录后台，否则后面的权限全部无效！</small>",
        "members,role*" => "允许管理操作员角色",
        "members,user*" => "允许管理操作员",
        "tamperproof,*" => "允许管理防篡改系统",
        "database,*" => "允许管理数据库",
        "modules,*" => "允许管理系统模块",
    );

    // 定义后台不需要权限的内容
    $_M->public_permissions_skip = array(
        "manage,login" => "登录界面",
        "check,login" => "登录程序",
        "check,logout" => "退出登录",
        "check,info" => "后台检查是否有新的消息",
        "log,login" => "查看登陆日志",
        "index,main" => "后台主界面",
    );
}

function _generate_pagenav($totalnum, $pagenum, $current_page, $link){
    $totalpage = ceil($totalnum / $pagenum);
    $html = "<ul class=\"pagination justify-content-center\">";
    $html .= "<li class=\"page-item" . ($current_page == 1 ? " disabled" : "") . "\"><a class=\"page-link\" href=\"javascript:;\" onclick=\"_goto('{$link}&page=1')\" aria-label=\"Previous\"><span aria-hidden=\"true\">&laquo;</span><span class=\"sr-only\">Previous</span></a></li>";

    $first_page = $current_page - 5;
    if($first_page < 1) $first_page = 1;
    for($i=$first_page; ($i<=$totalpage && $i < $first_page + 8); $i++) {
        $html .= "<li class=\"page-item" . ($i == $current_page ? " active" : "") . "\"><a class=\"page-link\" href=\"javascript:;\" onclick=\"_goto('{$link}&page={$i}')\">{$i}</a></li>";
    }
    $html .= "<li class=\"page-item" . ($current_page == $totalpage ? " disabled" : "") . "\"><a class=\"page-link\" href=\"javascript:;\" onclick=\"_goto('{$link}&page={$totalpage}')\" aria-label=\"Next\"><span aria-hidden=\"true\">&raquo;</span><span class=\"sr-only\">Next</span></a></li>";
    $html .= "</ul>";
    return $html;
}

/**
 * 管理页面的面包屑导航和工具栏
 * @param array $breadcrumb array(title => url)
 */
function _generate_breadcrumb($breadcrumb = array()){
    global $_M;
    $keys = array_keys($breadcrumb);
    $lastkey = end($keys);

    $breadcrumb_html = "";
    foreach($breadcrumb as $title=>$url) {
        if($title == $lastkey) {
            $breadcrumb_html .= "<li class=\"breadcrumb-item active\">{$title}</li>";
        } else {
            $breadcrumb_html .= "<li class=\"breadcrumb-item\"><a href=\"javascript:;\">{$title}</a></li>";
        }
    }

    if(!is_object($_M->template)) $_M->template();
    $template_name = $_M->template->current_template;
    $_M->template->load(MANAGE_PATH. "/templates/common/main_page_breadcrumb");
    $_M->template->set_var("{breadcrumb_html}", $breadcrumb_html);
    $result = $_M->template->display(true);
    $_M->template->current_template = $template_name;
    return $result;
}


/**
 * 记录管理日志 
 * @param string $module		模板
 * @param string $action	方法
 * @param string $args		附加参数，可选
 * @param boolean $successful	是否执行成功
 */
function logs_write($module, $action, $args="", $successful=true){
	global $_M;
	// 不记录状态
	if($module=="index" || $module=="check")
		return;

	$server = intval($_M->cfg["logs"]["server"] ?? 0);
	$database = $_M->cfg["logs"]["database"] ?? "";
	$schema = $_M->cfg["logs"]["schema"] ?? "";
	$table = $_M->cfg["logs"]["table"] ?? "";
	if(!isset($_M->cfg["db"][$server])) return;
	$server_info = $_M->cfg["db"][$server];

	// 如果日志服务器配置为0或未配置则不使用日志功能
	if(!$server || !strlen($table)) return;

	$data_id = $data_cid = 0;
	if(sizeof($_GET)) {
		$data_id = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
		$data_cid = isset($_GET["cid"]) ? intval($_GET["cid"]) : 0;
	}
	if(sizeof($_POST)){
		$data_id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
		$data_cid = isset($_POST["cid"]) ? intval($_POST["cid"]) : 0;
	}

	$data = array(
			"uid" => $_M->userinfo["id"] ?? 0,
			"username" => $_M->userinfo["username"] ?? "",
			"m" => $module,
			"a" => $action,
			"args" => $args,
			"status" => $server_info["dbtype"] == "mysql" ? ($successful ? 1 : 0) : ($successful ? "true" : "false"),
			"data" => json_encode(array("get"=>$_GET, "post"=>$_POST), JSON_UNESCAPED_UNICODE),
			"data_id" => $data_id,
			"data_cid" => $data_cid,
			"ip" => get_client_ip(),
            "http_user_agent" => get_http_user_agent(),
		);

	$_M->db($server);
	$_M->db->insert($data, array("table" => "{$database}." . $server_info["tablepre"] . "{$table}"));
	if($server != DEFAULT_DB) $_M->db(DEFAULT_DB);
}









